import 'package:backend.purchases/domain/entities/yookassa_api/payments/yookassa_api_canceled_payment.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/payments/canceled_payment/yookassa_api_payment_cancellation_details_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/payments/yookassa_api_payment_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/payments/yookassa_api_payment_meta_dto.dart';
import 'package:backend.purchases/infrastructure/dtos/yookassa_api/yookassa_api_money_amount_dto.dart';

const kYookassaApiCanceledPaymentCancellationDetailsKey = 'cancellation_details';

const kYookassaApiCanceledPaymentTypeValue = 'canceled';

class YookassaApiCanceledPaymentDto extends YookassaApiPaymentDto {
  const YookassaApiCanceledPaymentDto._internal({
    required this.id,
    required this.amountDto,
    required this.metadataDto,
    required this.test,
    required this.cancellationDetailsDto,
  });

  @override
  final String id;

  @override
  final YookassaApiMoneyAmountDto amountDto;

  @override
  final YookassaApiPaymentMetadataDto metadataDto;

  @override
  final bool test;

  final YookassaApiPaymentCancellationDetailsDto cancellationDetailsDto;

  @override
  String get type => kYookassaApiCanceledPaymentTypeValue;

  @override
  List<Object?> get props {
    return [
      id,
      amountDto,
      metadataDto,
      test,
      cancellationDetailsDto,
    ];
  }

  factory YookassaApiCanceledPaymentDto.fromJson(Map<String, dynamic> json) {
    final metadataJson = json[kYookassaApiPaymentMetadataKey];
    final metadataDto = YookassaApiPaymentMetadataDto.fromJson(metadataJson);

    final amountJson = json[kYookassaApiPaymentAmountKey];
    final amountDto = YookassaApiMoneyAmountDto.fromJson(amountJson);

    final cancellationDetailsJson = json[kYookassaApiCanceledPaymentCancellationDetailsKey];
    final cancellationDetailsDto =
        YookassaApiPaymentCancellationDetailsDto.fromJson(cancellationDetailsJson);

    return YookassaApiCanceledPaymentDto._internal(
      id: json[kYookassaApiPaymentIdKey],
      amountDto: amountDto,
      metadataDto: metadataDto,
      test: json[kYookassaApiPaymentTestKey],
      cancellationDetailsDto: cancellationDetailsDto,
    );
  }

  @override
  YookassaApiCanceledPayment toEntity() {
    final amount = amountDto.toEntity();
    final cancellationDetails = cancellationDetailsDto.toEntity();

    return YookassaApiCanceledPayment(
      id: id,
      amount: amount,
      purchaseItemSku: metadataDto.purchaseItemSku,
      userId: metadataDto.userId,
      savedPaymentMethodId: metadataDto.savedPaymentMethodId,
      test: test,
      cancellationDetails: cancellationDetails,
    );
  }
}
